<?php
/* ******************************
 *	Transfer aJax actions
 * ******************************
 */
include('../../includes/classes/core.php');

$action	    = $_REQUEST['act'];
$user_id	= $_SESSION['USERID'];
$rec_id		= $_REQUEST['id'];
$count      = $_REQUEST['count'];
$hidden     = $_REQUEST['hidden'];
$barcode    	   = $_REQUEST[barcode];
$this_id		   = $_REQUEST[this_id];
$comment		       = $_REQUEST[comment];
$first_weight	   = $_REQUEST[first_weight];
$current_weight	   = $_REQUEST[current_weight];
$error	= '';
$data	= '';

switch ($action) {
    case 'get_edit_page':
		$page		= GetPage($rec_id);
        $data 		= array('page'	=> $page);
        break;
    case 'get_add_page':
            $page		= Get_add_Page();
            $data 		= array('page'	=> $page);
        break;
    case 'barcode':

            $rResult = mysql_query("SELECT  warehouse_detail.id, barcode
                                    FROM `warehouse_detail`
                                    JOIN warehouse ON warehouse_detail.warehouse_id=warehouse.id
                                    WHERE warehouse_detail.actived=1  AND a_letter_id='$_REQUEST[letter]'
                                    ");
            while ( $aRow = mysql_fetch_array( $rResult ) )
            {

                $data .= "<option value='$aRow[0]'>$aRow[1]</option>";
            }
         echo($data);
         return 0;
        break;
    case 'get_list':
	    $rResult = mysql_query("SELECT 	        warehouse_detail.id,
                                				barcode,
                                				CONCAT('ბ  - ',warehouse.a_letter_id),
                                				CONCAT(client.`name`,' - ',client_objects.`name`),
                                				warehouse_status.`name`
                                FROM `warehouse_detail`
                                JOIN warehouse			ON warehouse_detail.warehouse_id	=	warehouse.id AND warehouse.actived=1
                                JOIN client_objects ON warehouse.client_object_id			=	client_objects.id AND client_objects.actived=1
                                JOIN client					ON client_objects.client_id				=	client.id AND client.actived=1
                                JOIN a_letter 			ON a_letter.id 										=	warehouse.a_letter_id AND a_letter.actived=1
                                JOIN warehouse_status ON warehouse_detail.`status`		=	warehouse_status.id
                                WHERE warehouse_detail.type=2 AND warehouse_detail.actived=1 AND a_letter.`status` IN (2,3,4, 5) AND a_letter.id>3700");

		$data = array(
			"aaData"	=> array()
		);

		if(!$rResult){
			$error = 'Invalid query: ' . mysql_error();
		}else{
			while ( $aRow = mysql_fetch_array( $rResult ) )
			{
				$row = array();
				for ( $i = 0 ; $i < $count ; $i++ )
				{
					/* General output */
					$row[] = $aRow[$i];
				}
				$data['aaData'][] = $row;
			}

		}

        break;
     case 'get_list1':
            $rResult = mysql_query("
                                                SELECT
                                                b_letter_details.warehouse_detail_id,
                                                b_letter_details.date,
                                				CONCAT('ბ - ', b_letter_details.a_letter_id) AS `letter`,
                                				persons.`name`,
                                				COUNT(part1.id) AS toneti,
                                				COUNT(part2.id) AS barabani,
                                				COUNT(part3.id) AS magnituri,
                                				COUNT(part4.id) as rezini,
                                				COUNT(part5.id) as gamwmendi,
                                				COUNT(part6.id) as dozirebis,
                                				COUNT(part7.id) AS chipi,
                                				COUNT(part8.id) AS korpusi,
                                		    COUNT(part9.id) AS not_service
                                FROM `b_letter_details`
                                JOIN  a_letter on b_letter_details.a_letter_id=a_letter.id
                                LEFT JOIN  b_letter_details part1 ON  part1.part_type_id=2  AND part1.id =b_letter_details.id
                                LEFT JOIN  b_letter_details part2 ON  part2.part_type_id=3  AND part2.id =b_letter_details.id
                                LEFT JOIN  b_letter_details part3 ON  part3.part_type_id=4  AND part3.id =b_letter_details.id
                                LEFT JOIN  b_letter_details part4 ON  part4.part_type_id=5  AND part4.id =b_letter_details.id
                                LEFT JOIN  b_letter_details part5 ON  part5.part_type_id=6  AND part5.id =b_letter_details.id
                                LEFT JOIN  b_letter_details part6 ON  part6.part_type_id=7  AND part6.id =b_letter_details.id
                                LEFT JOIN  b_letter_details part7 ON  part7.part_type_id=8  AND part7.id =b_letter_details.id
                                LEFT JOIN  b_letter_details part8 ON  part8.part_type_id=9  AND part8.id =b_letter_details.id
                                LEFT JOIN  b_letter_details part9 ON  part9.part_type_id=16 AND part9.id =b_letter_details.id
                                LEFT JOIN  persons ON b_letter_details.person_id=persons.id
                                WHERE b_letter_details.barcode=$barcode
                                GROUP BY b_letter_details.a_letter_id, b_letter_details.barcode");
            $data = array(
                "aaData"	=> array()
            );

            if(!$rResult){
                $error = 'Invalid query: ' . mysql_error();
            }else{
                while ( $aRow = mysql_fetch_array( $rResult ) )
                {

                    $data['aaData'][] = $aRow;
                }


            }

            break;
    case 'save-dialog':

       mysql_query("INSERT INTO `guarantee`
                    (`warehouse_detail_id`, create_date,`current_weight`,filler_id)
                    VALUES 	($this_id,           NOW(),       $current_weight,$user_id)
                    ON DUPLICATE KEY
                    UPDATE create_date= NOW(), current_weight=$current_weight, filler_id=$user_id");
       mysql_query("UPDATE `warehouse_detail` SET `comment`='$comment' WHERE (`id`='$this_id')");
     //   $error = 'ინფორმაცია დამახსოვრებულია';

        break;
    case 'save-dialog1':
        mysql_query("UPDATE `warehouse_detail` SET `type`='2' WHERE (`id`='$_REQUEST[wd_id]')");
        //   $error = 'ინფორმაცია დამახსოვრებულია';

        break;
    case 'disable':
        mysql_query("UPDATE `warehouse_detail` SET `type`='1' WHERE (`id`='$_REQUEST[this_id]')");
        //   $error = 'ინფორმაცია დამახსოვრებულია';

        break;
    default:
       $error = 'Action is Null';
}

$data['error'] = $error;

echo json_encode($data);


/* ******************************
 *	Transfer Functions
 * ******************************
 */
function GetPersons($person_id='')
{
    $req = mysql_query("SELECT persons.`name`
                        FROM persons
                        JOIN users ON users.person_id=persons.id
                        WHERE users.id=$person_id  AND persons.actived=1
        ");
    $aRow = mysql_fetch_array( $req );
    return $aRow[0];
}
function Get_add_Page(){
    $rResult = mysql_query("SELECT  a_letter.id,
                                    CONCAT('ბ - ',a_letter.id)
                            FROM `a_letter`
                            WHERE a_letter.`status` IN (2,3,4,5) AND a_letter.actived=1 AND id>3700");
    $data = '
	  <div id="dialog-form">
	    <fieldset>
	    	<legend>ძირითადი ინფორმაცია</legend>
			<table border="0" width="400px">
				<tr align="center">
					<th align="center" width=""><label for="">ბარათი</label></th>
					<th align="center"><label for="object">შტრიხკოდი</label></th>
				</tr>
				<tr align="center" style="  height: 30px;">
				<th align="center" style=" position: fixed; z-index:9999"> <select id="latter"><option value=0>-------</option>';
    while ( $aRow = mysql_fetch_array( $rResult ) )
    {

        $data .= "<option value='$aRow[0]'>$aRow[1]</option>";
    }
        $data .= '</select> </th>
					<th align="center" style=" position: fixed; z-index:9999;margin: 0px 200px;">	<select id="wd_id"><option></option></select></th>
				</tr>
			</table>
		</fieldset>
    </div>
    <input type="hidden" id="act" value="save-dialog1" />
    ';

return $data;

}
function GetPage($rec_id){
    $user_id	= $_SESSION['USERID'];
    $rResult = mysql_query("SELECT 	        warehouse_detail.id,
                            				warehouse_detail.barcode AS barcode,
                            				production.`name` AS prod_name,
                                            guarantee.current_weight,
				                            guarantee.filler_id,
                            				a_letter.start_date AS create_date,
                                            warehouse_detail.`comment`,
                                            CONCAT('ბ - ',a_letter.id) AS latter,
                            				CONCAT(client.`name`,' - ',client_objects.`name`) AS OBJ
                            FROM warehouse_detail
                            JOIN warehouse 	ON warehouse_detail.warehouse_id=warehouse.id
                            JOIN a_letter 	ON warehouse.a_letter_id=a_letter.id
                       LEFT JOIN guarantee ON guarantee.warehouse_detail_id=warehouse_detail.id
                            JOIN production ON warehouse_detail.prod_id=production.id
                            JOIN client_objects ON a_letter.client_object_id=client_objects.id
                            JOIN client ON client_objects.client_id=client.id
                            WHERE warehouse_detail.id='$rec_id'");
    $aRow1 = mysql_fetch_array($rResult);
    $aRow[filler_id]= GetPersons($user_id);
    $rResult1 = mysql_query("SELECT  DATE(a_letter.end_date) as end_date,
                            				a_letter.id,
                            				b_letter_details.quantity,
                                            warehouse_producing.tester_id,
                            				persons.`name` AS junior_technician_id
                            FROM  b_letter_details
                            JOIN persons ON b_letter_details.person_id=persons.id
                            JOIN a_letter ON a_letter.id =b_letter_details.a_letter_id
                            JOIN warehouse_producing ON warehouse_producing.warehouse_detail_id=b_letter_details.warehouse_detail_id
                            WHERE b_letter_details.barcode=$aRow1[barcode]
                            ORDER BY b_letter_details.id
                            DESC LIMIT 1");
    $rResult2=mysql_query("SELECT CONCAT(cartridge_parts_type.`name`,'')
                            FROM warehouse_producing
                            JOIN cartridge_parts_type ON warehouse_producing.cartridge_part_id=cartridge_parts_type.id
                            WHERE warehouse_producing.warehouse_detail_id=$rec_id AND cartridge_part_id NOT IN(12, 11)");
    while ($row = mysql_fetch_array($rResult2)) {
        $aRow2.=$row[0]."\n";
    }
                $aRow = array_merge($aRow1, mysql_fetch_array($rResult1));
	$data = '
	  <div id="dialog-form">
	    <fieldset>
	    	<legend>ძირითადი ინფორმაცია</legend>
			<table border="0" width="400px">
				<tr align="center">
					<th align="center" width=""><label for="">კლიენტი</label></th>
					<th align="center"><label for="object">შედგენის თარიღი</label></th>
				</tr>
				<tr align="center">
					<th align="center"> <input type="text" class="idls" id="OBJ" value="' . $aRow[OBJ] . '" style="width: 350px;"  disabled/></th>
					<th align="center">	<input type="text" class="idls" id="create_date" value="' . $aRow[create_date] . '"  disabled/></th>
				</tr>
			</table>
			<table border="0" width="400px">
			    <tr align="center">
					<th align="center" width=""><label for="">შტრიხკოდი</label></th>
					<th align="center"><label for="object">მოდელი</label></th>
					<th align="center"><label for="object">ბარათის ნომერი</label></th>
				</tr>
				<tr align="center">
					<th align="center"> <input type="text" id="barcode" class="idls" value="' . $aRow[barcode] . '" disabled/></th>
					<th align="center">	<input type="text" id="prod_name" class="idls" value="' . $aRow[prod_name] . '" disabled/></th>
					<th align="center">	<input type="text" id="latter" class="idls" value="' . $aRow[latter] . '" disabled/></th>
				</tr>
				<tr align="center">
					<th align="center" width="300px"><label for="">მიტანის თარიღი</label></th>
					<th align="center" width=""><label for="">უმცროსი ტექნიკოსი</label></th>
					<th  align="center"><label for="object">უფროსი ტექნიკოსი</label></th>
				</tr>
				<tr align="center">
					<th align="center"> <input type="text" id="end_date" class="idls" value="' . $aRow[end_date] . '" disabled/></th>
					<th align="center">	<input type="text" id="junior_technician_id" class="idls" value="' . $aRow[junior_technician_id] . '" disabled/></th>
					<th align="center">	<input type="text" id="senior_technician_id" class="idls" value="' . GetPersons($aRow[tester_id]) . '" disabled/></th>
				</tr>
				<tr align="center">
					<th align="center" width="300px"><label for="">შეავსო</label></th>
					<th align="center" width=""><label for="">საწყისი წონა</label></th>
					<th  align="center"><label for="object">არსებული წონა</label></th>
				</tr>
				<tr align="center">
					<th align="center"><input id="filler_id" class="idls " value="'. GetPersons($aRow[filler_id]) .'" disabled/></th>
					<th align="center"><input type="number" id="first_weight"   min="0" max="1" step="0.001" class="idls" value="'. $aRow[quantity] .'"disabled></th>
					<th align="center"><input type="number" id="current_weight" min="0" max="1" step="0.001" class="idls" value="'. $aRow[current_weight] .'"></th>
				</tr>
			</table>
        </fieldset>

		<fieldset>
			<table border="0" width="400px">
				<tr align="center">
					<th align="center" width="442px; "><label for="">ხარვეზის აღწერილობა</label></th>
					<th align="center"><label for="object">შეკეთების აღწერა</label></th>
				</tr>
				<tr align="center">
					<th align="center"><textarea style="width: 342px; height: 50px;" class="idls" id="comment" >' . $aRow[comment] . '</textarea></th>
					<th align="center"><textarea style="height: 50px;"  class="idls"  disabled>' . $aRow2 . '</textarea></th>
				</tr>
			    <tr align="center">
					<th align="center"><input type="button" id="disable" style="display: inline-block;" class="idls" value="საგარანტიოს გაუქმება" /><input type="button" style="display: inline-block;"  id="print" class="idls" value="ბეჭდვა" /></th>
					<th align="center"></th>
				</tr>
			</table>

		</fieldset>
					    <br />
		<fieldset style="width: 400px;">
		    	<legend>ისტორია</legend>
			    <div class="inner-table">
				    <div id="dt_example" class="ex_highlight_row">
				        <div id="container" class="overhead_container">
				            <div id="dynamic">
				                <table class="display" id="inner_list">
				                    <thead>
				                        <tr id="datatable_header" style="height: 80px">
				                            <th>ID</th>
				                            <th style="width: 141px">თარიღი</th>
				                            <th style="width: 68px">ბარათის ნომერი</th>
					                        <th style="width: 165px">ტექნიკოსი</th>
											<th style="width: 20px"><span class="rotate" style="font-size: 10px; padding: 35px 7px;">ტონერი</span></th>
					                        <th style="width: 20px"><span class="rotate" style="font-size: 10px; padding: 35px 7px;">ბარაბანი</span></th>
					                        <th style="width: 20px"><span class="rotate" style="font-size: 10px; padding: 35px 7px;">მაგნიტური</span></th>
					                        <th style="width: 20px"><span class="rotate" style="font-size: 10px; padding: 35px 7px;">რეზინი</span></th>
					                        <th style="width: 20px"><span class="rotate" style="font-size: 10px; padding: 35px 7px;">დოზირების</span></th>
					                        <th style="width: 20px"><span class="rotate" style="font-size: 10px; padding: 35px 7px;">ჩიპი</span></th>
					                        <th style="width: 20px"><span class="rotate" style="font-size: 10px; padding: 35px 7px;">კოპუსი</span></th>
					                        <th style="width: 20px"><span class="rotate" style="font-size: 10px; padding: 35px 7px;">სერვისის გარეშე</span></th>
				                        </tr>
				                    </thead>
				                </table>
				            </div>
				        </div>
				    </div>
				</div>
			</fieldset>
		<input type="hidden" id="this_id" value="'.$_REQUEST[id].'" />
		<input type="hidden" id="act" value="save-dialog" />
    </div>
    ';

	return $data;
}
?>